<style type="text/css">
#teste
{
    padding: 0;
    margin: 0;
}
#cas
{
    padding-left: 1%;
    padding-bottom: 0;
    border: 0;
}
.tabbar {
  margin-top: 10px;
  border-bottom: 3px solid #000;
  padding: 2px 0px;
  font-family: sans-serif;
  font-size: 83%;
}
.tabbar ul, li {
  margin: 0;
  padding: 0;
  display: inline;
  list-style: none;
}
.tabbar a {
  padding: 2px 1em;
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
  border-bottom: none;
  background-color: #ffffff;
  color: #000;
  text-decoration: none;
}
.tabbar a:hover, .tabbar a:active {
  border-top: 1px solid blue;
  border-left: 1px solid blue;
  border-right: 1px solid blue;
  background-color: #fff;
  color: blue;
  text-decoration: underline;
}
.tabbar a.tabact {
  border-bottom: 1px solid #000;
  background-color: #000;
  color: white;
  font-weight: bold;
}
.tabbar a.tabact:hover,
.tabbar a.tabact:active {
  background-color: #000;
  color: white;
}
</style>
<?php
    $xml = "./bibliotecas/scilab_valid.xml";

    if (isset($_GET['topico']) && isset($_GET['item']))
    {
        $dom = new DomDocument();
        $dom->load($xml);
        $subjects = $dom->getelementsbytagname("subject");
        if ($subjects->length > $_GET['topico'])
        {
            $items = $subjects->item($_GET['topico'])->getelementsbytagname("item");
            $assunto = $subjects->item($_GET['topico'])->getattribute("name");
            if ($items->length > $_GET['item'])
            {
                $item = $items->item($_GET['item']);
                $nome = $item->getattribute("name");
                $desc = $item->getelementsbytagname("description")->item(0)->nodeValue;
                $relcoms = $item->getElementsByTagname("command");
?>
        <h1><?php echo $assunto ?></h2>
        <h2><?php echo $nome ?></h2>
<?php
?>
<div id="teste">
<?php
                if (!empty($desc))
                {
?>
        <p><strong><?php echo $msg->get('Descricao') ?>:</strong> <?php echo $desc ?></p>
<?php
                }
                $i = false;
                $diretorios = array(0 => "programming", 1 => "graphics", 2 => "elementary", 3 => "fileio",
                    4 => "functions", 5 => "strings", 6 => "gui", 7 => "utilities", 8 => "linear",
                    9 => "polynomials", 10 => "control", 11 => "robust", 12 => "nonlinear",
                    13 => "signal", 14 => "arma", 15 => "metanet", 16 => "scicos",
                    17 => "sound", 18 => "translation", 19 => "tdcs", 20 => "statistics",
                    21 => "dcd", 22 => "identification", 23 => "mtlb", 24 => "sparse",
                    25 => "pvm", 26 => "tksci", 27 => "java");
            
                foreach ($relcoms as $c)
                {
                    $x = $c->nodeValue;
                    if (!empty($x))
                    {
                        if (!$i)
                            echo "<p><strong>".$msg->get('Comandos_Relacionados').":</strong> ";
                        else
                            echo ", ";
                        $i = true;
                        //echo $c->nodeValue;
                        $help = explode("/",$c->nodeValue);
                        if (count($help) == 2)
                        {
                            $pleh = explode(".",$help[1]);
                            $pleh = $pleh[0];
?>
                <a href="#" onClick="window.open('ajuda/scilab/eng/<?php echo $diretorios[$help[0]]."/".$help[1] ?>', 'Ajuda', 'toolbar=no, location=no, directories=no, scrollbars=yes,resizable=yes,menubar=no,width=500,height=350')"><?php echo $pleh ?></a>
<?php
                        }
                    }
                }
                if ($i) echo "</p>";
?>
<div class="tabbar">
    <ul>
    <li><a href="index.php?inc=cas_load_system_scilab2.php&execucao=1&topico=<?php echo $_GET['topico'] ?>&item=<?php echo $_GET['item'] ?>"<?php if ($_GET['execucao'] == 1) echo " class=\"tabact\""; ?>><?php echo $msg->get('Exemplo') ?></a></li>
    <li><a href="index.php?inc=cas_load_system_scilab2.php&execucao=2&topico=<?php echo $_GET['topico'] ?>&item=<?php echo $_GET['item'] ?>"<?php if ($_GET['execucao'] == 2) echo " class=\"tabact\""; ?>><?php echo $msg->get('Exercicio') ?></a></li>
    <li><a href="index.php?inc=cas_load_system_scilab2.php&execucao=3&topico=<?php echo $_GET['topico'] ?>&item=<?php echo $_GET['item'] ?>"<?php if ($_GET['execucao'] == 3) echo " class=\"tabact\""; ?>><?php echo $msg->get('Tente_Voce_Mesmo') ?></a></li>
    </ul>
</div>
<div id="cas">
<?php
                if ($_GET['execucao'] == 1)
                {
                    $i = 0;
                    $example = $item->getelementsbytagname("example");
                    $example = $example->item(0);
                    $params = $example->getelementsbytagname("sentence");
                    $remark = $example->getelementsbytagname("remark");
                    $remark = $remark->item($remark->length - 1)->nodeValue; 
                    foreach ($params as $p)
                        $param[$i++] = $p->nodeValue;
            
                    include("config.php");
                    $descriptorspec = array(
                        0 => array("pipe", "r"),
                        1 => array("pipe", "w"),
			//                        2 => array("file", $DOCUMENT_ROOT."/error-scilab-output.txt", "a")
			2 => array("pipe","w")
                    );
            
                    $env = array('HOME' => $DOCUMENT_ROOT,'DISPLAY' => '0');
            
                    $process = proc_open('scilab -nogui', $descriptorspec, $pipes, $cwd, $env);
                    if (is_resource($process)) {
                        $str1 = array("%","'");
                        $str2 = array("\%","''");
                        for ($i=0; $i<count($param);$i++)
                            fwrite($pipes[0], "printf(' ".str_replace($str1,$str2,$param[$i])."\\n');".$param[$i]."\n");
                        fwrite($pipes[0],"exit\n");
                        fclose($pipes[0]);
                        echo "<pre>\n"; 
                        echo stream_get_contents($pipes[1]);
                        echo "\n</pre>\n";
                        fclose($pipes[1]);
                        $return_value = proc_close($process);
                    }
                    if (!empty($remark))
                    {
?>
                <strong><?php echo $msg->get('Comentario') ?>:</strong> <?php echo $remark ?>
<?php
                    }
                }
                elseif ($_GET['execucao'] == 2)
                {
                    $exer = $item->getelementsbytagname("exercises")->item(0);
                    $descrip = $exer->getelementsbytagname("description")->item(0)->nodeValue;
                    $hint = $exer->getelementsbytagname("hint")->item(0)->nodeValue;
?>
            <p><strong><?php echo $msg->get('Descricao') ?></strong>: <?php echo $descrip ?></p>
            <p><strong><?php echo $msg->get('Dica') ?></strong>: <?php echo $hint ?></p>
<?php
                }
            }
        }
    }
?>
</div>
</div>
